﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace RCT2
{
    public class PositionHelper
    {
        //[DllImport("shell32.dll")]
        //public static extern System.UInt32 SHAppBarMessage(System.UInt32 dwMessage, ref APPBARDATA data);
        //[DllImport("user32.dll")]
        //private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        //[StructLayout(LayoutKind.Sequential)]
        //public struct APPBARDATA
        //{
        //    public uint cbSize;
        //    public IntPtr hWnd;
        //    public uint uCallbackMessage;
        //    public uint uEdge;
        //    public Rectangle rc;
        //    public int lParam;
        //}

        //private const int ABM_GETTASKBARPOS = 0x00000005;


        #region Native API
        // The native API functions/enums/structs needed to access taskbar info
        // consult MSDN for more detailed information about them
        [StructLayout(LayoutKind.Sequential)]
        private struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct APPBARDATA
        {
            public Int32 cbSize;
            public IntPtr hWnd;
            public Int32 uCallbackMessage;
            public ABEdge uEdge;
            public RECT rc;
            public IntPtr lParam;
        }

        private enum ABMsg
        {
            ABM_GETTASKBARPOS = 5
        }

        private enum ABEdge
        {
            Left = 0,
            Top = 1,
            Right = 2,
            Bottom = 3,
        }

        [DllImport("shell32.dll")]
        private static extern IntPtr SHAppBarMessage(ABMsg dwMessage, [In, Out] ref APPBARDATA pData);

        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        #endregion


        public PositionHelper()
        {

        }

        public Point TaskbarPosition()
        {
            APPBARDATA appBar = GetTaskBarData();
            return new Point(appBar.rc.left, appBar.rc.top);
        }
        public Size TaskbarSize()
        {
            APPBARDATA appBar = GetTaskBarData();
            return new Size(appBar.rc.right - appBar.rc.left, appBar.rc.bottom - appBar.rc.top);
        }
        APPBARDATA GetTaskBarData()
        {
            APPBARDATA appBar = CreateAppBarData();

            System.IntPtr ret = SHAppBarMessage(ABMsg.ABM_GETTASKBARPOS, ref appBar); //ABM_GETTASKBARPOS = 5
            return appBar;
        }
        public int TaskbarSide()
        {
            APPBARDATA appBar = GetTaskBarData();
            if (appBar.uEdge == ABEdge.Left) return 1;
            else if (appBar.uEdge == ABEdge.Top) return 2;
            else if (appBar.uEdge == ABEdge.Right) return 3;
            else return 0;
        }
        private static APPBARDATA CreateAppBarData()
        {
            APPBARDATA appBar = new APPBARDATA();
            appBar.hWnd = FindWindow("Shell_TrayWnd", "");
            appBar.cbSize = Marshal.SizeOf(appBar);
         
            return appBar;
        }
        //public int TaskbarSide() //0 is bottom, 1 is left, 2 is top, 3 is right
        //{
        //    int location = 0;
        //    bool topBottom = (Screen.PrimaryScreen.WorkingArea.Width == Screen.PrimaryScreen.Bounds.Width);
        //    if (topBottom)
        //    {
        //        if (Screen.PrimaryScreen.WorkingArea.Top != 0)
        //            location = 2;
        //    }
        //    else
        //    {
        //        if (Screen.PrimaryScreen.WorkingArea.Left == 0)
        //            location = 3;
        //        else
        //            location = 1;
        //    }
        //    return location;
        //}



        //public Point TaskbarPosition()
        //{
        //    APPBARDATA data = new APPBARDATA();
        //    data.cbSize = (uint)Marshal.SizeOf(data.GetType());
        //    uint result = SHAppBarMessage(ABM_GETTASKBARPOS, ref data);

        //    if (result == 1)
        //        return data.rc.Location;
        //    else
        //        throw new InvalidOperationException();
        //}

        

        //public Size TaskbarSize()
        //{
        //    //int side = TaskbarSide();
        //    //int width, height;
        //    //if (side == 0 || side == 2) { width = Screen.PrimaryScreen.Bounds.Width; height = Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height; }
        //    //else { width = Screen.PrimaryScreen.Bounds.Width - Screen.PrimaryScreen.WorkingArea.Width; height = Screen.PrimaryScreen.Bounds.Height; }
        //    //return new Size(width, height);
        //}
    }
}